package com.sy.service.impl.inOutWarehouse;

import com.sy.bean.inventory.InOutWarehouse.OutInWarehouse;
import com.sy.bean.inventory.InOutWarehouse.OutInWarehouseDetail;
import com.sy.bean.inventory.InOutWarehouse.param.UpdateHouseParam;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.inventory.OutInWarehouseMapper;
import com.sy.service.inventory.inOutWarehouse.OutInWarehouseService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OutInWarehouseServiceImpl extends BaseServiceImpl<OutInWarehouse, OutInWarehouseMapper> implements OutInWarehouseService {
    @Autowired
    OutInWarehouseMapper outInWarehouseMapper;

    @Override
    public OutInWarehouseDetail queryDetail(int id) {
        return outInWarehouseMapper.queryDetail(id);
    }

    @Transactional
    @Override
    public int outHouse(UpdateHouseParam param) {
        //先修改出入库表
        int rs = outInWarehouseMapper.update(param);
        //失败
        if (rs < 1)
            throw new RuntimeException("修改失败");
        //成功 查询关于出入库表的详情数
        rs = outInWarehouseMapper.countDetail(param.getId());
        //如果没有 直接新增
        if (rs == 0) {
            rs = outInWarehouseMapper.saveDetail(param.getId(), param.getGoodsDetails());
            // 小于1 修改失败
            if (rs < 1)
                throw new RuntimeException("修改失败");
        }
        //如果有 先删除
        rs = outInWarehouseMapper.deleteByInId(param.getId());
        if (rs < 1)
            throw new RuntimeException("修改失败");
        // 删除成功 添加
        rs = outInWarehouseMapper.saveDetail(param.getId(), param.getGoodsDetails());
        if (rs < 1)
            throw new RuntimeException("修改失败");
        return 1;
    }

    @Transactional
    @Override
    public int delete(int id) {
        //先删除出入库表信息
        int rs = outInWarehouseMapper.delete(id);
        //失败
        if (rs < 1)
            throw new RuntimeException("删除失败");
        //查看详情数
        rs = outInWarehouseMapper.countDetail(id);
        //没有 直接删除成功
        if (rs == 0) {
            return 1;
        }
        //有 删除
        rs = outInWarehouseMapper.deleteByInId(id);
        if (rs < 1)
            throw new RuntimeException("删除失败");
        return 1;
    }
}
